#include "..\\code\\dataStruct.h"
#include "..\\code\\common.h"
#include <math.h>

void calIdmax(tranDevice* trandevice);
void phaseCalIdmax(tranDevice* trandevice);

void tranLonDiff(tranDevice* trandevice);

void tranDeltaDiffStart(tranDevice* trandevice);
void tranDeltaCal(tranDevice* trandevice);

void tranRecordMemory(tranDevice* trandevice);

void recordMemoryhalf(tranDevice* trandevice);

void tranFre(tranDevice* trandevice);

void tranCalFre(tranDevice* trandevice);

void overExc(tranDevice* trandevice);

void zeroStart(tranDevice* trandevice);

void impStarth(tranDevice* trandevice);
void impStartv(tranDevice* trandevice);

void lonDiffStart(tranDevice* trandevice);

void splitStart(tranDevice* trandevice);

void zeroCurStarth(tranDevice* trandevice);
void zeroCurStartv(tranDevice* trandevice);
void zeroCurStartt(tranDevice* trandevice);

void phaseCurStarth(tranDevice* trandevice);
void phaseCurStartv(tranDevice* trandevice);
void phaseCurStartl1(tranDevice* trandevice);
void phaseCurStartl2(tranDevice* trandevice);

void tranPTerror(tranDevice* trandevice);
void tranCTerror(tranDevice* trandevice);

void calIdmax(tranDevice* trandevice){

    double voltageH = trandevice->ratedV10;
    double voltageV = trandevice->ratedV20;
    double voltageL = trandevice->ratedV30;

    Phasor Iah, Ibh, Ich;
    Phasor Iav, Ibv, Icv;
    Phasor Ial, Ibl, Icl;
    Phasor Ida, Idb, Idc;
    double Idmax;


    Iah = phasorSub(trandevice->phasor[3],trandevice->phasor[5]);
    Ibh = phasorSub(trandevice->phasor[4],trandevice->phasor[3]);
    Ich = phasorSub(trandevice->phasor[5],trandevice->phasor[4]);

    Iah = phasorNumMulti(trandevice->ct1, Iah);
    Ibh = phasorNumMulti(trandevice->ct1, Ibh);
    Ich = phasorNumMulti(trandevice->ct1, Ich);

    Iav = phasorSub(trandevice->phasor[9],trandevice->phasor[11]);
    Ibv = phasorSub(trandevice->phasor[10],trandevice->phasor[9]);
    Icv = phasorSub(trandevice->phasor[11],trandevice->phasor[10]);

    Iav = phasorNumMulti(trandevice->ct2, Iav);
    Ibv = phasorNumMulti(trandevice->ct2, Ibv);
    Icv = phasorNumMulti(trandevice->ct2, Icv);

    Iav = phasorNumMulti((double)(voltageV / voltageH), Iav);
    Ibv = phasorNumMulti((double)(voltageV / voltageH), Ibv);
    Icv = phasorNumMulti((double)(voltageV / voltageH), Icv);

    Ial = phasorNumMulti((double)(voltageL / voltageH), trandevice->phasor[15]);
    Ibl = phasorNumMulti((double)(voltageL / voltageH), trandevice->phasor[16]);
    Icl = phasorNumMulti((double)(voltageL / voltageH), trandevice->phasor[17]);

    Ial = phasorNumMulti(trandevice->ct3, Ial);
    Ibl = phasorNumMulti(trandevice->ct3, Ibl);
    Icl = phasorNumMulti(trandevice->ct3, Icl);

    Ida = phasorAdd(Iah, Iav);
    Ida = phasorAdd(Ida, Ial);
    Idb = phasorAdd(Ibh, Ibv);
    Idb = phasorAdd(Idb, Ibl);
    Idc = phasorAdd(Ich, Icv);
    Idc = phasorAdd(Idc, Icl);


    Idmax = phasorAbs(Ida);
    if (phasorAbs(Idb) > Idmax) {
        Idmax = phasorAbs(Idb);
    }
    if (phasorAbs(Idc) > Idmax) {
        Idmax = phasorAbs(Idc);
    }

    trandevice->Idt = Idmax;

    trandevice->Idta = phasorAbs(Ida);
    trandevice->Idtb = phasorAbs(Idb);
    trandevice->Idtc = phasorAbs(Idc);

    trandevice->tranProRes[3] = Idmax;

}


void phaseCalIdmax(tranDevice* trandevice){

    double voltageH = trandevice->ratedV10;
    double voltageV = trandevice->ratedV20;
    double voltageL = trandevice->ratedV30;

    Phasor Iah, Ibh, Ich;
    Phasor Iav, Ibv, Icv;
    Phasor Ial, Ibl, Icl;
    Phasor Ida, Idb, Idc;
    double Idmax;
//    Phasor I0h, I0v, I0l;

    Iah = phasorSub(trandevice->phasor[3],trandevice->phasor[5]);
    Ibh = phasorSub(trandevice->phasor[4],trandevice->phasor[3]);
    Ich = phasorSub(trandevice->phasor[5],trandevice->phasor[4]);

    Iah = phasorNumMulti(trandevice->ct1, Iah);
    Ibh = phasorNumMulti(trandevice->ct1, Ibh);
    Ich = phasorNumMulti(trandevice->ct1, Ich);

    Iav = phasorSub(trandevice->phasor[9],trandevice->phasor[11]);
    Ibv = phasorSub(trandevice->phasor[10],trandevice->phasor[9]);
    Icv = phasorSub(trandevice->phasor[11],trandevice->phasor[10]);

    Iav = phasorNumMulti(trandevice->ct2, Iav);
    Ibv = phasorNumMulti(trandevice->ct2, Ibv);
    Icv = phasorNumMulti(trandevice->ct2, Icv);

    Iav = phasorNumMulti((double)(voltageV / voltageH), Iav);
    Ibv = phasorNumMulti((double)(voltageV / voltageH), Ibv);
    Icv = phasorNumMulti((double)(voltageV / voltageH), Icv);

    Ial = phasorSub(trandevice->phasor[18], trandevice->phasor[20]);
    Ibl = phasorSub(trandevice->phasor[19], trandevice->phasor[18]);
    Icl = phasorSub(trandevice->phasor[20], trandevice->phasor[19]);

    Ial = phasorNumMulti(trandevice->ct4, Ial);
    Ibl = phasorNumMulti(trandevice->ct4, Ibl);
    Icl = phasorNumMulti(trandevice->ct4, Icl);

    Ial = phasorNumMulti((voltageL / voltageH), Ial);
    Ibl = phasorNumMulti((voltageL / voltageH), Ibl);
    Icl = phasorNumMulti((voltageL / voltageH), Icl);

    Ida = phasorAdd(Iah, Iav);
    Ida = phasorAdd(Ida, Ial);
    Idb = phasorAdd(Ibh, Ibv);
    Idb = phasorAdd(Idb, Ibl);
    Idc = phasorAdd(Ich, Icv);
    Idc = phasorAdd(Idc, Icl);

    Idmax = phasorAbs(Ida);
    if (phasorAbs(Idb) > Idmax) {
        Idmax = phasorAbs(Idb);
    }
    if (phasorAbs(Idc) > Idmax) {
        Idmax = phasorAbs(Idc);
    }

    trandevice->Idt_phase = Idmax;
    trandevice->Idta_phase = phasorAbs(Ida);
    trandevice->Idtb_phase = phasorAbs(Idb);
    trandevice->Idtc_phase = phasorAbs(Idc);

}



void tranRecordMemory(tranDevice* trandevice) {
    // 记录当前时刻
    int i;
    for (i = 3*POINTS; i > 2*POINTS; i--){
        inst2phasor(trandevice->filterVa1, i, &trandevice->memoryVa1[3*POINTS-i]);  inst2phasor(trandevice->filterIa1, i, &trandevice->memoryIa1[3*POINTS-i]);
        inst2phasor(trandevice->filterVb1, i, &trandevice->memoryVb1[3*POINTS-i]);  inst2phasor(trandevice->filterIb1, i, &trandevice->memoryIb1[3*POINTS-i]);
        inst2phasor(trandevice->filterVc1, i, &trandevice->memoryVc1[3*POINTS-i]);  inst2phasor(trandevice->filterIc1, i, &trandevice->memoryIc1[3*POINTS-i]);

        inst2phasor(trandevice->filterVa2, i, &trandevice->memoryVa2[3*POINTS-i]);  inst2phasor(trandevice->filterIa2, i, &trandevice->memoryIa2[3*POINTS-i]);
        inst2phasor(trandevice->filterVb2, i, &trandevice->memoryVb2[3*POINTS-i]);  inst2phasor(trandevice->filterIb2, i, &trandevice->memoryIb2[3*POINTS-i]);
        inst2phasor(trandevice->filterVc2, i, &trandevice->memoryVc2[3*POINTS-i]);  inst2phasor(trandevice->filterIc2, i, &trandevice->memoryIc2[3*POINTS-i]);

        inst2phasor(trandevice->filterVa3, i, &trandevice->memoryVa3[3*POINTS-i]);  inst2phasor(trandevice->filterIa3, i, &trandevice->memoryIa3[3*POINTS-i]);
        inst2phasor(trandevice->filterVb3, i, &trandevice->memoryVb3[3*POINTS-i]);  inst2phasor(trandevice->filterIb3, i, &trandevice->memoryIb3[3*POINTS-i]);
        inst2phasor(trandevice->filterVc3, i, &trandevice->memoryVc3[3*POINTS-i]);  inst2phasor(trandevice->filterIc3, i, &trandevice->memoryIc3[3*POINTS-i]);

        inst2phasor(trandevice->filterVa4, i, &trandevice->memoryIa4[3*POINTS-i]);  inst2phasor(trandevice->filterIa4, i, &trandevice->memoryIa5[3*POINTS-i]);
        inst2phasor(trandevice->filterVb4, i, &trandevice->memoryIb4[3*POINTS-i]);  inst2phasor(trandevice->filterIb4, i, &trandevice->memoryIb5[3*POINTS-i]);
        inst2phasor(trandevice->filterVc4, i, &trandevice->memoryIc4[3*POINTS-i]);  inst2phasor(trandevice->filterIc4, i, &trandevice->memoryIc5[3*POINTS-i]);

    }
}


void recordMemoryhalf(tranDevice* trandevice) {
    // 记录当前时刻
    int i;
    int k = 3;
    for (i = 3*POINTS; i > 2*POINTS; i--){
        halfWaveFourier(trandevice->filterVa1, i, &trandevice->halfmemoryVa1[k*POINTS-i]);  halfWaveFourier(trandevice->filterIa1, i, &trandevice->halfmemoryIa1[k*POINTS-i]);
        halfWaveFourier(trandevice->filterVb1, i, &trandevice->halfmemoryVb1[k*POINTS-i]);  halfWaveFourier(trandevice->filterIb1, i, &trandevice->halfmemoryIb1[k*POINTS-i]);
        halfWaveFourier(trandevice->filterVc1, i, &trandevice->halfmemoryVc1[k*POINTS-i]);  halfWaveFourier(trandevice->filterIc1, i, &trandevice->halfmemoryIc1[k*POINTS-i]);

        halfWaveFourier(trandevice->filterVa2, i, &trandevice->halfmemoryVa2[k*POINTS-i]);  halfWaveFourier(trandevice->filterIa2, i, &trandevice->halfmemoryIa2[k*POINTS-i]);
        halfWaveFourier(trandevice->filterVb2, i, &trandevice->halfmemoryVb2[k*POINTS-i]);  halfWaveFourier(trandevice->filterIb2, i, &trandevice->halfmemoryIb2[k*POINTS-i]);
        halfWaveFourier(trandevice->filterVc2, i, &trandevice->halfmemoryVc2[k*POINTS-i]);  halfWaveFourier(trandevice->filterIc2, i, &trandevice->halfmemoryIc2[k*POINTS-i]);

        halfWaveFourier(trandevice->filterVa3, i, &trandevice->halfmemoryVa3[k*POINTS-i]);  halfWaveFourier(trandevice->filterIa3, i, &trandevice->halfmemoryIa3[k*POINTS-i]);
        halfWaveFourier(trandevice->filterVb3, i, &trandevice->halfmemoryVb3[k*POINTS-i]);  halfWaveFourier(trandevice->filterIb3, i, &trandevice->halfmemoryIb3[k*POINTS-i]);
        halfWaveFourier(trandevice->filterVc3, i, &trandevice->halfmemoryVc3[k*POINTS-i]);  halfWaveFourier(trandevice->filterIc3, i, &trandevice->halfmemoryIc3[k*POINTS-i]);

        halfWaveFourier(trandevice->filterVa4, i, &trandevice->halfmemoryIa4[k*POINTS-i]);  halfWaveFourier(trandevice->filterIa4, i, &trandevice->halfmemoryIa5[k*POINTS-i]);
        halfWaveFourier(trandevice->filterVb4, i, &trandevice->halfmemoryIb4[k*POINTS-i]);  halfWaveFourier(trandevice->filterIb4, i, &trandevice->halfmemoryIb5[k*POINTS-i]);
        halfWaveFourier(trandevice->filterVc4, i, &trandevice->halfmemoryIc4[k*POINTS-i]);  halfWaveFourier(trandevice->filterIc4, i, &trandevice->halfmemoryIc5[k*POINTS-i]);

    }
}

void tranLonDiff(tranDevice* trandevice){
    //稳态差动启动值
    double Icdqd = (trandevice->tranStartSetValue[0]) * (trandevice->ct1);
    double Idmax;
    double time;

    time = trandevice->time;

    Idmax = trandevice->Idt;

    if ((Idmax > Icdqd) && (time > 0.1)) {
        trandevice->lonDiffStartFlag = 1;
        trandevice->startTime[0] = trandevice->time;
        tranWriteLog(trandevice, "稳态差流启动(纵差)");
    }
}

void tranPhaseLonDiff(tranDevice* trandevice){
    //稳态差动启动值
    double Icdqd = trandevice->tranStartSetValue[8]* (trandevice->ct1);
    double Idmax;
    double time;

    time = trandevice->time;

    Idmax = trandevice->Idt_phase;
//    trandevice->tranProRes[4] = Idmax;

    if ((Idmax > Icdqd) && (time > 0.1)) {
        trandevice->phaseLonDiffStartFlag = 1;
        trandevice->startTime[7] = trandevice->time;
        tranWriteLog(trandevice, "稳态差流启动(分相)");
    }
}


void tranDeltaDiffStart(tranDevice* trandevice){
    //稳态差动启动值
    double time = trandevice->time;
    double Idtset = 1.25*(trandevice->tranStartSetValue[3]) * (trandevice->ct1);

    double voltageH = trandevice->ratedV10;
    double voltageV = trandevice->ratedV20;
    double voltageL = trandevice->ratedV30;

    double Ida_base, Idb_base, Idc_base;
    double Ira_base, Irb_base, Irc_base;

    double Ifah, Ifbh, Ifch;
    double Ifav, Ifbv, Ifcv;
    double Ifal, Ifbl, Ifcl;
    double Ifda, Ifdb, Ifdc;

    Ifah = trandevice->halfIa1[0] - trandevice->halfIc1[0];
    Ifbh = trandevice->halfIb1[0] - trandevice->halfIa1[0];
    Ifch = trandevice->halfIc1[0] - trandevice->halfIb1[0];

    Ifah = trandevice->ct1 * Ifah;
    Ifbh = trandevice->ct1 * Ifbh;
    Ifch = trandevice->ct1 * Ifch;

    Ifav = trandevice->halfIa2[0] - trandevice->halfIc2[0];
    Ifbv = trandevice->halfIb2[0] - trandevice->halfIa2[0];
    Ifcv = trandevice->halfIc2[0] - trandevice->halfIb2[0];

    Ifav = trandevice->ct2 * Ifav;
    Ifbv = trandevice->ct2 * Ifbv;
    Ifcv = trandevice->ct2 * Ifcv;

    Ifav = (double)(voltageV / voltageH)* Ifav;
    Ifbv = (double)(voltageV / voltageH)* Ifbv;
    Ifcv = (double)(voltageV / voltageH)* Ifcv;

    Ifal = (double)(voltageL / voltageH)* trandevice->halfIa3[0];
    Ifbl = (double)(voltageL / voltageH)* trandevice->halfIb3[0];
    Ifcl = (double)(voltageL / voltageH)* trandevice->halfIc3[0];

    Ifal = trandevice->ct3 * Ifal;
    Ifbl = trandevice->ct3 * Ifbl;
    Ifcl = trandevice->ct3 * Ifcl;

    Ifah = fabs(Ifah);
    Ifbh = fabs(Ifbh);
    Ifch = fabs(Ifch);
    Ifav = fabs(Ifav);
    Ifbv = fabs(Ifbv);
    Ifcv = fabs(Ifcv);
    Ifal = fabs(Ifal);
    Ifbl = fabs(Ifbl);
    Ifcl = fabs(Ifcl);

    Ifda = Ifah + Ifav + Ifal;
    Ifdb = Ifbh + Ifbv + Ifbl;
    Ifdc = Ifch + Ifcv + Ifal;

    double Ifahm, Ifbhm, Ifchm;
    double Ifavm, Ifbvm, Ifcvm;
    double Ifalm, Ifblm, Ifclm;
    double Ifdam, Ifdbm, Ifdcm;

    Ifahm = trandevice->halfIa1m[0] - trandevice->halfIc1m[0];
    Ifbhm = trandevice->halfIb1m[0] - trandevice->halfIa1m[0];
    Ifchm = trandevice->halfIc1m[0] - trandevice->halfIb1m[0];

    Ifahm = trandevice->ct1 * Ifahm;
    Ifbhm = trandevice->ct1 * Ifbhm;
    Ifchm = trandevice->ct1 * Ifchm;

    Ifavm = trandevice->halfIa2m[0] - trandevice->halfIc2m[0];
    Ifbvm = trandevice->halfIb2m[0] - trandevice->halfIa2m[0];
    Ifcvm = trandevice->halfIc2m[0] - trandevice->halfIb2m[0];

    Ifavm = trandevice->ct2 * Ifavm;
    Ifbvm = trandevice->ct2 * Ifbvm;
    Ifcvm = trandevice->ct2 * Ifcvm;

    Ifavm = (double)(voltageV / voltageH)* Ifavm;
    Ifbvm = (double)(voltageV / voltageH)* Ifbvm;
    Ifcvm = (double)(voltageV / voltageH)* Ifcvm;

    Ifalm = (double)(voltageL / voltageH)* trandevice->halfIa3m[0];
    Ifblm = (double)(voltageL / voltageH)* trandevice->halfIb3m[0];
    Ifclm = (double)(voltageL / voltageH)* trandevice->halfIc3m[0];

    Ifalm = trandevice->ct3 * Ifalm;
    Ifblm = trandevice->ct3 * Ifblm;
    Ifclm = trandevice->ct3 * Ifclm;

    Ifahm = fabs(Ifahm);
    Ifbhm = fabs(Ifbhm);
    Ifchm = fabs(Ifchm);
    Ifavm = fabs(Ifavm);
    Ifbvm = fabs(Ifbvm);
    Ifcvm = fabs(Ifcvm);
    Ifalm = fabs(Ifalm);
    Ifblm = fabs(Ifblm);
    Ifclm = fabs(Ifclm);

    Ifdam = Ifahm + Ifavm + Ifalm;
    Ifdbm = Ifbhm + Ifbvm + Ifblm;
    Ifdcm = Ifchm + Ifcvm + Ifalm;

    Ida_base = fabs(Ifdam - Ifda);
    Idb_base = fabs(Ifdbm - Ifdb);
    Idc_base = fabs(Ifdcm - Ifdc);

    Ira_base = fabs(Ifah - Ifahm) + fabs(Ifav - Ifavm) + fabs(Ifal - Ifalm);
    Irb_base = fabs(Ifbh - Ifbhm) + fabs(Ifbv - Ifbvm) + fabs(Ifbl - Ifblm);
    Irc_base = fabs(Ifch - Ifchm) + fabs(Ifcv - Ifcvm) + fabs(Ifcl - Ifclm);

    trandevice->tranProRes[4] = Ida_base;
//


//
    if (((Ida_base > Idtset) || (Idb_base > Idtset) || (Idc_base > Idtset)) && (time > 0.1)) {
        trandevice->deltaDiffStartFlag = 1;
        trandevice->startTime[1] = trandevice->time;

        tranWriteLog(trandevice, "工频变化量差流启动(纵差)");
    }

}


void tranPhaseDeltaStart(tranDevice* trandevice){
    //稳态差动启动值
    double time = trandevice->time;
    double Idtset = 1.25*(trandevice->tranStartSetValue[10]) *(trandevice->ct1);

    double voltageH = trandevice->ratedV10;
    double voltageV = trandevice->ratedV20;
    double voltageL = trandevice->ratedV30;

    double Ida_base, Idb_base, Idc_base;
    double Ira_base, Irb_base, Irc_base;

    double Ifah, Ifbh, Ifch;
    double Ifav, Ifbv, Ifcv;
    double Ifal, Ifbl, Ifcl;
    double Ifda, Ifdb, Ifdc;

    Ifah = trandevice->halfIa1[0] - trandevice->halfIc1[0];
    Ifbh = trandevice->halfIb1[0] - trandevice->halfIa1[0];
    Ifch = trandevice->halfIc1[0] - trandevice->halfIb1[0];
    Ifah = trandevice->ct1 * Ifah;
    Ifbh = trandevice->ct1 * Ifbh;
    Ifch = trandevice->ct1 * Ifch;

    Ifav = trandevice->halfIa2[0] - trandevice->halfIc2[0];
    Ifbv = trandevice->halfIb2[0] - trandevice->halfIa2[0];
    Ifcv = trandevice->halfIc2[0] - trandevice->halfIb2[0];
    Ifav = trandevice->ct2 * Ifav;
    Ifbv = trandevice->ct2 * Ifbv;
    Ifcv = trandevice->ct2 * Ifcv;

    Ifav = (double)(voltageV / voltageH)* Ifav;
    Ifbv = (double)(voltageV / voltageH)* Ifbv;
    Ifcv = (double)(voltageV / voltageH)* Ifcv;

    Ifal = (double)(voltageL / voltageH)* (trandevice->halfIa4[0] - trandevice->halfIc4[0]);
    Ifbl = (double)(voltageL / voltageH)* (trandevice->halfIb4[0] - trandevice->halfIa4[0]);
    Ifcl = (double)(voltageL / voltageH)* (trandevice->halfIc4[0] - trandevice->halfIb4[0]);
    Ifal = trandevice->ct4 * Ifal;
    Ifbl = trandevice->ct4 * Ifbl;
    Ifcl = trandevice->ct4 * Ifcl;

    Ifah = fabs(Ifah);
    Ifbh = fabs(Ifbh);
    Ifch = fabs(Ifch);

    Ifav = fabs(Ifav);
    Ifbv = fabs(Ifbv);
    Ifcv = fabs(Ifcv);

    Ifal = fabs(Ifal);
    Ifbl = fabs(Ifbl);
    Ifcl = fabs(Ifcl);

    Ifda = Ifah + Ifav + Ifal;
    Ifdb = Ifbh + Ifbv + Ifbl;
    Ifdc = Ifch + Ifcv + Ifcl;

    double Ifahm, Ifbhm, Ifchm;
    double Ifavm, Ifbvm, Ifcvm;
    double Ifalm, Ifblm, Ifclm;
    double Ifdam, Ifdbm, Ifdcm;

    Ifahm = trandevice->halfIa1m[0] - trandevice->halfIc1m[0];
    Ifbhm = trandevice->halfIb1m[0] - trandevice->halfIa1m[0];
    Ifchm = trandevice->halfIc1m[0] - trandevice->halfIb1m[0];
    Ifahm = trandevice->ct1 * Ifahm;
    Ifbhm = trandevice->ct1 * Ifbhm;
    Ifchm = trandevice->ct1 * Ifchm;

    Ifavm = trandevice->halfIa2m[0] - trandevice->halfIc2m[0];
    Ifbvm = trandevice->halfIb2m[0] - trandevice->halfIa2m[0];
    Ifcvm = trandevice->halfIc2m[0] - trandevice->halfIb2m[0];
    Ifavm = trandevice->ct2 * Ifavm;
    Ifbvm = trandevice->ct2 * Ifbvm;
    Ifcvm = trandevice->ct2 * Ifcvm;

    Ifavm = (double)(voltageV / voltageH)* Ifavm;
    Ifbvm = (double)(voltageV / voltageH)* Ifbvm;
    Ifcvm = (double)(voltageV / voltageH)* Ifcvm;

    Ifalm = (double)(voltageL / voltageH)* (trandevice->halfIa4m[0] - trandevice->halfIc4m[0]);
    Ifblm = (double)(voltageL / voltageH)* (trandevice->halfIb4m[0] - trandevice->halfIa4m[0]);
    Ifclm = (double)(voltageL / voltageH)* (trandevice->halfIc4m[0] - trandevice->halfIb4m[0]);
    Ifalm = trandevice->ct4 * Ifalm;
    Ifblm = trandevice->ct4 * Ifblm;
    Ifclm = trandevice->ct4 * Ifclm;

    Ifahm = fabs(Ifahm);
    Ifbhm = fabs(Ifbhm);
    Ifchm = fabs(Ifchm);
    Ifavm = fabs(Ifavm);
    Ifbvm = fabs(Ifbvm);
    Ifcvm = fabs(Ifcvm);
    Ifalm = fabs(Ifalm);
    Ifblm = fabs(Ifblm);
    Ifclm = fabs(Ifclm);

    Ifdam = Ifahm + Ifavm + Ifalm;
    Ifdbm = Ifbhm + Ifbvm + Ifblm;
    Ifdcm = Ifchm + Ifcvm + Ifclm;

    Ida_base = fabs(Ifdam - Ifda);
    Idb_base = fabs(Ifdbm - Ifdb);
    Idc_base = fabs(Ifdcm - Ifdc);

    Ira_base = fabs(Ifah - Ifahm) + fabs(Ifav - Ifavm) + fabs(Ifal - Ifalm);
    Irb_base = fabs(Ifbh - Ifbhm) + fabs(Ifbv - Ifbvm) + fabs(Ifbl - Ifblm);
    Irc_base = fabs(Ifch - Ifchm) + fabs(Ifcv - Ifcvm) + fabs(Ifcl - Ifclm);
//
////    trandevice->test[0] = Ida_base;
////    trandevice->test[1] = phasorAbs(Ifda);
//    trandevice->tranProRes[8] = Ida_base;
//    trandevice->tranProRes[9] = Idb_base;
//    trandevice->tranProRes[10] = Idc_base;
//
//    trandevice->Ida_base = Ida_base;
//    trandevice->Idb_base = Idb_base;
//    trandevice->Idc_base = Idc_base;
//    trandevice->Ira_base = Ira_base;
//    trandevice->Irb_base = Irb_base;
//    trandevice->Irc_base = Irc_base;
//
    if (((Ida_base > Idtset) || (Idb_base > Idtset) || (Idc_base > Idtset)) && (time > 0.1)) {
        trandevice->phaseDeltaStartFlag = 1;
        trandevice->startTime[8] = trandevice->time;

        tranWriteLog(trandevice, "工频变化量差流启动(分相)");
    }

}


void tranDeltaCal(tranDevice* trandevice){

    double voltageH = trandevice->ratedV10;
    double voltageV = trandevice->ratedV20;
    double voltageL = trandevice->ratedV30;

    double Ida_base, Idb_base, Idc_base;
    double Ira_base, Irb_base, Irc_base;

    Phasor Ifah, Ifbh, Ifch;
    Phasor Ifav, Ifbv, Ifcv;
    Phasor Ifal, Ifbl, Ifcl;
    Phasor Ifda, Ifdb, Ifdc;

    Ifah = phasorSub(trandevice->phasor[3],trandevice->phasor[5]);
    Ifbh = phasorSub(trandevice->phasor[4],trandevice->phasor[3]);
    Ifch = phasorSub(trandevice->phasor[5],trandevice->phasor[4]);

    Ifah = phasorNumMulti(trandevice->ct1, Ifah);
    Ifbh = phasorNumMulti(trandevice->ct1, Ifbh);
    Ifch = phasorNumMulti(trandevice->ct1, Ifch);

    Ifav = phasorSub(trandevice->phasor[9],trandevice->phasor[11]);
    Ifbv = phasorSub(trandevice->phasor[10],trandevice->phasor[9]);
    Ifcv = phasorSub(trandevice->phasor[11],trandevice->phasor[10]);

    Ifav = phasorNumMulti((double)(voltageV / voltageH), Ifav);
    Ifbv = phasorNumMulti((double)(voltageV / voltageH), Ifbv);
    Ifcv = phasorNumMulti((double)(voltageV / voltageH), Ifcv);

    Ifav = phasorNumMulti(trandevice->ct2, Ifav);
    Ifbv = phasorNumMulti(trandevice->ct2, Ifbv);
    Ifcv = phasorNumMulti(trandevice->ct2, Ifcv);

    Ifal = phasorNumMulti((double)(voltageL / voltageH), trandevice->phasor[15]);
    Ifbl = phasorNumMulti((double)(voltageL / voltageH), trandevice->phasor[16]);
    Ifcl = phasorNumMulti((double)(voltageL / voltageH), trandevice->phasor[17]);

    Ifal = phasorNumMulti(trandevice->ct3, Ifal);
    Ifbl = phasorNumMulti(trandevice->ct3, Ifbl);
    Ifcl = phasorNumMulti(trandevice->ct3, Ifcl);

    Ifda = phasorAdd(Ifah, Ifav);
    Ifda = phasorAdd(Ifda, Ifal);
    Ifdb = phasorAdd(Ifbh, Ifbv);
    Ifdb = phasorAdd(Ifdb, Ifbl);
    Ifdc = phasorAdd(Ifch, Ifcv);
    Ifdc = phasorAdd(Ifdc, Ifcl);

    Phasor Ifahm, Ifbhm, Ifchm;
    Phasor Ifavm, Ifbvm, Ifcvm;
    Phasor Ifalm, Ifblm, Ifclm;
    Phasor Ifdam, Ifdbm, Ifdcm;

    Ifahm = phasorSub(trandevice->memoryIa1[0], trandevice->memoryIc1[0]);
    Ifbhm = phasorSub(trandevice->memoryIb1[0], trandevice->memoryIa1[0]);
    Ifchm = phasorSub(trandevice->memoryIc1[0], trandevice->memoryIb1[0]);

    Ifahm = phasorNumMulti(trandevice->ct1, Ifahm);
    Ifbhm = phasorNumMulti(trandevice->ct1, Ifbhm);
    Ifchm = phasorNumMulti(trandevice->ct1, Ifchm);

    Ifavm = phasorSub(trandevice->memoryIa2[0], trandevice->memoryIc2[0]);
    Ifbvm = phasorSub(trandevice->memoryIb2[0], trandevice->memoryIa2[0]);
    Ifcvm = phasorSub(trandevice->memoryIc2[0], trandevice->memoryIb2[0]);

    Ifavm = phasorNumMulti(trandevice->ct2, Ifavm);
    Ifbvm = phasorNumMulti(trandevice->ct2, Ifbvm);
    Ifcvm = phasorNumMulti(trandevice->ct2, Ifcvm);

    Ifavm = phasorNumMulti((double)(voltageV / voltageH), Ifavm);
    Ifbvm = phasorNumMulti((double)(voltageV / voltageH), Ifbvm);
    Ifcvm = phasorNumMulti((double)(voltageV / voltageH), Ifcvm);

    Ifalm = phasorNumMulti((double)(voltageL / voltageH), trandevice->memoryIa3[0]);
    Ifblm = phasorNumMulti((double)(voltageL / voltageH), trandevice->memoryIb3[0]);
    Ifclm = phasorNumMulti((double)(voltageL / voltageH), trandevice->memoryIc3[0]);

    Ifalm = phasorNumMulti(trandevice->ct3, Ifalm);
    Ifblm = phasorNumMulti(trandevice->ct3, Ifblm);
    Ifclm = phasorNumMulti(trandevice->ct3, Ifclm);

    Ifdam = phasorAdd(Ifahm, Ifavm);
    Ifdam = phasorAdd(Ifdam, Ifalm);
    Ifdbm = phasorAdd(Ifbhm, Ifbvm);
    Ifdbm = phasorAdd(Ifdbm, Ifblm);
    Ifdcm = phasorAdd(Ifchm, Ifcvm);
    Ifdcm = phasorAdd(Ifdcm, Ifclm);

    Ida_base = phasorAbs(phasorSub(Ifdam, Ifda));
    Idb_base = phasorAbs(phasorSub(Ifdbm, Ifdb));
    Idc_base = phasorAbs(phasorSub(Ifdcm, Ifdc));

    Ira_base = phasorAbs(phasorSub(Ifah, Ifahm))+phasorAbs(phasorSub(Ifav, Ifavm))+phasorAbs(phasorSub(Ifal, Ifalm));
    Irb_base = phasorAbs(phasorSub(Ifbh, Ifbhm))+phasorAbs(phasorSub(Ifbv, Ifbvm))+phasorAbs(phasorSub(Ifbl, Ifblm));
    Irc_base = phasorAbs(phasorSub(Ifch, Ifchm))+phasorAbs(phasorSub(Ifcv, Ifcvm))+phasorAbs(phasorSub(Ifcl, Ifclm));

    trandevice->Ida_base = Ida_base;
    trandevice->Idb_base = Idb_base;
    trandevice->Idc_base = Idc_base;
    trandevice->Ira_base = Ira_base;
    trandevice->Irb_base = Irb_base;
    trandevice->Irc_base = Irc_base;

}


void tranPhaseDeltaCal(tranDevice* trandevice){

    double voltageH = trandevice->ratedV10;
    double voltageV = trandevice->ratedV20;
    double voltageL = trandevice->ratedV30;

    double Ida_base, Idb_base, Idc_base;
    double Ira_base, Irb_base, Irc_base;

    Phasor Ifah, Ifbh, Ifch;
    Phasor Ifav, Ifbv, Ifcv;
    Phasor Ifal, Ifbl, Ifcl;
    Phasor Ifda, Ifdb, Ifdc;

    Ifah = phasorSub(trandevice->phasor[3],trandevice->phasor[5]);
    Ifbh = phasorSub(trandevice->phasor[4],trandevice->phasor[3]);
    Ifch = phasorSub(trandevice->phasor[5],trandevice->phasor[4]);

    Ifah = phasorNumMulti(trandevice->ct1, Ifah);
    Ifbh = phasorNumMulti(trandevice->ct1, Ifbh);
    Ifch = phasorNumMulti(trandevice->ct1, Ifch);

    Ifav = phasorSub(trandevice->phasor[9],trandevice->phasor[11]);
    Ifbv = phasorSub(trandevice->phasor[10],trandevice->phasor[9]);
    Ifcv = phasorSub(trandevice->phasor[11],trandevice->phasor[10]);

    Ifav = phasorNumMulti(trandevice->ct2, Ifav);
    Ifbv = phasorNumMulti(trandevice->ct2, Ifbv);
    Ifcv = phasorNumMulti(trandevice->ct2, Ifcv);

    Ifav = phasorNumMulti((double)(voltageV / voltageH), Ifav);
    Ifbv = phasorNumMulti((double)(voltageV / voltageH), Ifbv);
    Ifcv = phasorNumMulti((double)(voltageV / voltageH), Ifcv);

    Ifal = phasorSub(trandevice->phasor[18], trandevice->phasor[20]);
    Ifbl = phasorSub(trandevice->phasor[19], trandevice->phasor[18]);
    Ifcl = phasorSub(trandevice->phasor[20], trandevice->phasor[19]);

    Ifal = phasorNumMulti(trandevice->ct4, Ifal);
    Ifbl = phasorNumMulti(trandevice->ct4, Ifbl);
    Ifcl = phasorNumMulti(trandevice->ct4, Ifcl);

    Ifal = phasorNumMulti((voltageL / voltageH), Ifal);
    Ifbl = phasorNumMulti((voltageL / voltageH), Ifbl);
    Ifcl = phasorNumMulti((voltageL / voltageH), Ifcl);

    Ifda = phasorAdd(Ifah, Ifav);
    Ifda = phasorAdd(Ifda, Ifal);
    Ifdb = phasorAdd(Ifbh, Ifbv);
    Ifdb = phasorAdd(Ifdb, Ifbl);
    Ifdc = phasorAdd(Ifch, Ifcv);
    Ifdc = phasorAdd(Ifdc, Ifcl);

    Phasor Ifahm, Ifbhm, Ifchm;
    Phasor Ifavm, Ifbvm, Ifcvm;
    Phasor Ifalm, Ifblm, Ifclm;
    Phasor Ifdam, Ifdbm, Ifdcm;

    Ifahm = phasorSub(trandevice->memoryIa1[0], trandevice->memoryIc1[0]);
    Ifbhm = phasorSub(trandevice->memoryIb1[0], trandevice->memoryIa1[0]);
    Ifchm = phasorSub(trandevice->memoryIc1[0], trandevice->memoryIb1[0]);

    Ifahm = phasorNumMulti(trandevice->ct1, Ifahm);
    Ifbhm = phasorNumMulti(trandevice->ct1, Ifbhm);
    Ifchm = phasorNumMulti(trandevice->ct1, Ifchm);

    Ifavm = phasorSub(trandevice->memoryIa2[0], trandevice->memoryIc2[0]);
    Ifbvm = phasorSub(trandevice->memoryIb2[0], trandevice->memoryIa2[0]);
    Ifcvm = phasorSub(trandevice->memoryIc2[0], trandevice->memoryIb2[0]);

    Ifavm = phasorNumMulti(trandevice->ct2, Ifavm);
    Ifbvm = phasorNumMulti(trandevice->ct2, Ifbvm);
    Ifcvm = phasorNumMulti(trandevice->ct2, Ifcvm);

    Ifavm = phasorNumMulti((double)(voltageV / voltageH), Ifavm);
    Ifbvm = phasorNumMulti((double)(voltageV / voltageH), Ifbvm);
    Ifcvm = phasorNumMulti((double)(voltageV / voltageH), Ifcvm);

    Ifalm = phasorSub(trandevice->memoryIa4[0], trandevice->memoryIc4[0]);
    Ifblm = phasorSub(trandevice->memoryIb4[0], trandevice->memoryIa4[0]);
    Ifclm = phasorSub(trandevice->memoryIc4[0], trandevice->memoryIb4[0]);

    Ifalm = phasorNumMulti(trandevice->ct4, Ifalm);
    Ifblm = phasorNumMulti(trandevice->ct4, Ifblm);
    Ifclm = phasorNumMulti(trandevice->ct4, Ifclm);

    Ifalm = phasorNumMulti((double)(voltageL / voltageH), Ifalm);
    Ifblm = phasorNumMulti((double)(voltageL / voltageH), Ifblm);
    Ifclm = phasorNumMulti((double)(voltageL / voltageH), Ifclm);

    Ifdam = phasorAdd(Ifahm, Ifavm);
    Ifdam = phasorAdd(Ifdam, Ifalm);
    Ifdbm = phasorAdd(Ifbhm, Ifbvm);
    Ifdbm = phasorAdd(Ifdbm, Ifblm);
    Ifdcm = phasorAdd(Ifchm, Ifcvm);
    Ifdcm = phasorAdd(Ifdcm, Ifclm);

    Ida_base = phasorAbs(phasorSub(Ifdam, Ifda));
    Idb_base = phasorAbs(phasorSub(Ifdbm, Ifdb));
    Idc_base = phasorAbs(phasorSub(Ifdcm, Ifdc));

    Ira_base = phasorAbs(phasorSub(Ifah, Ifahm))+phasorAbs(phasorSub(Ifav, Ifavm))+phasorAbs(phasorSub(Ifal, Ifalm));
    Irb_base = phasorAbs(phasorSub(Ifbh, Ifbhm))+phasorAbs(phasorSub(Ifbv, Ifbvm))+phasorAbs(phasorSub(Ifbl, Ifblm));
    Irc_base = phasorAbs(phasorSub(Ifch, Ifchm))+phasorAbs(phasorSub(Ifcv, Ifcvm))+phasorAbs(phasorSub(Ifcl, Ifclm));

    trandevice->Idap_base = Ida_base;
    trandevice->Idbp_base = Idb_base;
    trandevice->Idcp_base = Idc_base;
    trandevice->Irap_base = Ira_base;
    trandevice->Irbp_base = Irb_base;
    trandevice->Ircp_base = Irc_base;

}



void zeroStart(tranDevice* trandevice){
    Phasor Iah, Ibh, Ich;
    Phasor Iav, Ibv, Icv;
    Phasor Iaw, Ibw, Icw;
    Phasor Ih0, Iv0, Iw0;
    Phasor I0all;
    double time;
    time = trandevice->time;

    Iah = trandevice->phasor[3];
    Ibh = trandevice->phasor[4];
    Ich = trandevice->phasor[5];

    Iah = phasorNumMulti(trandevice->ct1, Iah);
    Ibh = phasorNumMulti(trandevice->ct1, Ibh);
    Ich = phasorNumMulti(trandevice->ct1, Ich);

    Ih0 = phasorAdd(Iah, Ibh);
    Ih0 = phasorAdd(Ih0, Ich);

    Iav = trandevice->phasor[9];
    Ibv = trandevice->phasor[10];
    Icv = trandevice->phasor[11];

    Iav = phasorNumMulti(trandevice->ct2, Iav);
    Ibv = phasorNumMulti(trandevice->ct2, Ibv);
    Icv = phasorNumMulti(trandevice->ct2, Icv);

    Iv0 = phasorAdd(Iav, Ibv);
    Iv0 = phasorAdd(Iv0, Icv);

    Iaw = trandevice->phasor[21];
    Ibw = trandevice->phasor[22];
    Icw = trandevice->phasor[23];

    Iaw = phasorNumMulti(trandevice->ct5, Iaw);
    Ibw = phasorNumMulti(trandevice->ct5, Ibw);
    Icw = phasorNumMulti(trandevice->ct5, Icw);

    Iw0 = phasorAdd(Iaw, Ibw);
    Iw0 = phasorAdd(Iw0, Icw);

    I0all = phasorAdd(Ih0, Iv0);
    I0all = phasorAdd(I0all, Iw0);

    double I0qd = trandevice->tranStartSetValue[4] * (trandevice->ct1);
    if((phasorAbs(I0all) > 0.95*I0qd) && (time > 0.1)){
        trandevice->zeroDiffStartFlag = 1;
        trandevice->startTime[2] = trandevice->time;
        tranWriteLog(trandevice, "零序比例差动启动");
    }

}

void lowDiffStart(tranDevice* trandevice){
    double time;
    double Ixcd = trandevice->tranStartSetValue[5] * (trandevice->ct3);

    time = trandevice->time;

    Phasor Iaw, Ibw, Icw;
    Phasor Ial, Ibl, Icl;
    Phasor Iad, Ibd, Icd;
    double Idmax;

    Ial = trandevice->phasor[15];
    Ibl = trandevice->phasor[16];
    Icl = trandevice->phasor[17];

    Ial = phasorNumMulti(trandevice->ct3, Ial);
    Ibl = phasorNumMulti(trandevice->ct3, Ibl);
    Icl = phasorNumMulti(trandevice->ct3, Icl);

    Iaw = trandevice->phasor[18];
    Ibw = trandevice->phasor[19];
    Icw = trandevice->phasor[20];

    Iaw = phasorNumMulti(trandevice->ct4, Iaw);
    Ibw = phasorNumMulti(trandevice->ct4, Ibw);
    Icw = phasorNumMulti(trandevice->ct4, Icw);

    Iad = phasorSub(Ial, Iaw);
    Iad = phasorAdd(Iad, Icw);
    Ibd = phasorSub(Ibl, Ibw);
    Ibd = phasorAdd(Ibd, Iaw);
    Icd = phasorSub(Icl, Icw);
    Icd = phasorAdd(Icd, Ibw);

    Idmax = phasorAbs(Iad);
    if(Idmax<phasorAbs(Ibd)){
        Idmax = phasorAbs(Ibd);
    }
    if(Idmax<phasorAbs(Icd)){
        Idmax = phasorAbs(Icd);
    }

    if((Idmax > Ixcd) && (time > 0.1)){
        trandevice->lowDiffStartFlag = 1;
        trandevice->startTime[3] = trandevice->time;
        tranWriteLog(trandevice, "低压侧小区差动启动");
    }
}

void impStarth(tranDevice* trandevice){
    double time;
    double Ihset;
    double Ihe;
    Phasor Ifah, Ifbh, Ifch;

    time = trandevice->time;
    Ihe = 1.732 * 1.414 * trandevice->ratedI1 * (trandevice->ct1);
    Ihset= trandevice->tranStartSetValue[6] * (trandevice->ct1);

    Ifah = phasorSub(trandevice->phasor[3],trandevice->phasor[5]);
    Ifbh = phasorSub(trandevice->phasor[4],trandevice->phasor[3]);
    Ifch = phasorSub(trandevice->phasor[5],trandevice->phasor[4]);

    Ifah = phasorNumMulti(trandevice->ct1, Ifah);
    Ifbh = phasorNumMulti(trandevice->ct1, Ifbh);
    Ifch = phasorNumMulti(trandevice->ct1, Ifch);

    Phasor Ifahm, Ifbhm, Ifchm;

    Ifahm = phasorSub(trandevice->memoryIa1[0],trandevice->memoryIc1[0]);
    Ifbhm = phasorSub(trandevice->memoryIb1[0],trandevice->memoryIa1[0]);
    Ifchm = phasorSub(trandevice->memoryIc1[0],trandevice->memoryIb1[0]);

    Ifahm = phasorNumMulti(trandevice->ct1, Ifahm);
    Ifbhm = phasorNumMulti(trandevice->ct1, Ifbhm);
    Ifchm = phasorNumMulti(trandevice->ct1, Ifchm);

    double Ihda, Ihdb, Ihdc;

    Ihda = phasorAbs(phasorSub(Ifah, Ifahm));
    Ihdb = phasorAbs(phasorSub(Ifbh, Ifbhm));
    Ihdc = phasorAbs(phasorSub(Ifch, Ifchm));

    double Ihmax;
    Ihmax = Ihda;
    if(Ihmax < Ihdb){
        Ihmax = Ihdb;
    }
    if(Ihmax < Ihdc){
        Ihmax = Ihdc;
    }

    Phasor Ih2;
    Phasor Ih2a, Ih2b, Ih2c;

    Ih2a = Ifah;
    Ih2b = phasorContrarotate(Ifbh, 240.0);
    Ih2c = phasorContrarotate(Ifch, 120.0);
    Ih2 = phasorAdd(Ih2a, phasorAdd(Ih2b ,Ih2c));


    if((Ihmax > 1.25*Ihset) && (phasorAbs(Ih2) > 0.2*Ihe) && (time > 0.1)){
        trandevice->impStartFlag_h = 1;
        trandevice->startTime[4] = trandevice->time;
        tranWriteLog(trandevice, "高压侧阻抗保护启动");
    }

}

void impStartv(tranDevice* trandevice){

    double time;
    double Ivset;
    double Ive;
    Phasor Ifav, Ifbv, Ifcv;
    time = trandevice->time;
    Ive = 1.732 * 1.414 * trandevice->ratedI2;
    Ivset= trandevice->tranStartSetValue[7] * (trandevice->ct2);

    Ifav = phasorSub(trandevice->phasor[9],trandevice->phasor[11]);
    Ifbv = phasorSub(trandevice->phasor[10],trandevice->phasor[9]);
    Ifcv = phasorSub(trandevice->phasor[11],trandevice->phasor[10]);

    Ifav = phasorNumMulti(trandevice->ct2, Ifav);
    Ifbv = phasorNumMulti(trandevice->ct2, Ifbv);
    Ifcv = phasorNumMulti(trandevice->ct2, Ifcv);

    Phasor Ifavm, Ifbvm, Ifcvm;

    Ifavm = phasorSub(trandevice->memoryIa2[0],trandevice->memoryIc2[0]);
    Ifbvm = phasorSub(trandevice->memoryIb2[0],trandevice->memoryIa2[0]);
    Ifcvm = phasorSub(trandevice->memoryIc2[0],trandevice->memoryIb2[0]);

    Ifavm = phasorNumMulti(trandevice->ct2, Ifavm);
    Ifbvm = phasorNumMulti(trandevice->ct2, Ifbvm);
    Ifcvm = phasorNumMulti(trandevice->ct2, Ifcvm);

    double Ivda, Ivdb, Ivdc;

    Ivda = phasorAbs(phasorSub(Ifav, Ifavm));
    Ivdb = phasorAbs(phasorSub(Ifbv, Ifbvm));
    Ivdc = phasorAbs(phasorSub(Ifcv, Ifcvm));

    double  Ivmax;

    Ivmax = Ivda;
    if(Ivmax < Ivdb){
        Ivmax = Ivdb;
    }
    if(Ivmax < Ivdc){
        Ivmax = Ivdc;
    }

    Phasor Iv2;
    Phasor Iv2a, Iv2b, Iv2c;

    Iv2a = Ifav;
    Iv2b = phasorContrarotate(Ifbv, 240.0);
    Iv2c = phasorContrarotate(Ifcv, 120.0);
    Iv2 = phasorAdd(Iv2a, phasorAdd(Iv2b ,Iv2c));

//    trandevice->test[0] = Ivmax;
//    trandevice->test[1] = phasorAbs(Iv2);
//    trandevice->test[2] = 1.25*Ivset;
//    trandevice->test[3] = 0.6*Ive;

    if((Ivmax > 1.25*Ivset) && (phasorAbs(Iv2) > 0.2*Ive) && (time > 0.1)){
        trandevice->impStartFlag_v = 1;
        trandevice->startTime[5] = trandevice->time;
        tranWriteLog(trandevice, "中压侧阻抗保护启动");
    }



}

void splitStart(tranDevice* trandevice){
    double Ifcdqd = (trandevice->tranStartSetValue[2]) * (trandevice->ct1);

    Phasor Iah, Ibh, Ich;
    Phasor Iav, Ibv, Icv;
    Phasor Iaw, Ibw, Icw;
    Phasor Ida, Idb, Idc;

    double Ifda, Ifdb, Ifdc;
    double Idmax;
    double time;

    time = trandevice->time;

    Iah = trandevice->phasor[3];
    Ibh = trandevice->phasor[4];
    Ich = trandevice->phasor[5];

    Iah = phasorNumMulti(trandevice->ct1, Iah);
    Ibh = phasorNumMulti(trandevice->ct1, Ibh);
    Ich = phasorNumMulti(trandevice->ct1, Ich);

    Iav = trandevice->phasor[9];
    Ibv = trandevice->phasor[10];
    Icv = trandevice->phasor[11];

    Iav = phasorNumMulti(trandevice->ct2, Iav);
    Ibv = phasorNumMulti(trandevice->ct2, Ibv);
    Icv = phasorNumMulti(trandevice->ct2, Icv);

    Iaw = trandevice->phasor[21];
    Ibw = trandevice->phasor[22];
    Icw = trandevice->phasor[23];

    Iaw = phasorNumMulti(trandevice->ct5, Iaw);
    Ibw = phasorNumMulti(trandevice->ct5, Ibw);
    Icw = phasorNumMulti(trandevice->ct5, Icw);

    Ida = phasorAdd(Iah, Iav);
    Ida = phasorAdd(Ida, Iaw);
    Idb = phasorAdd(Ibh, Ibv);
    Idb = phasorAdd(Idb, Ibw);
    Idc = phasorAdd(Ich, Icv);
    Idc = phasorAdd(Idc, Icw);

    Ifda = phasorAbs(Ida);
    Ifdb = phasorAbs(Idb);
    Ifdc = phasorAbs(Idc);

    Idmax = Ifda;
    if(Idmax < Ifdb){
        Idmax = Ifdb;
    }
    if(Idmax < Ifdc){
        Idmax = Ifdc;
    }

    if((Idmax > Ifcdqd) && (time > 0.1)){
        trandevice->splitDiffStartFlag = 1;
        trandevice->startTime[6] = trandevice->time;
        tranWriteLog(trandevice, "分侧差动保护启动");

    }

}

void zeroCurStarth(tranDevice* trandevice){
    Phasor Ia, Ib, Ic;
    Phasor I0;
    double time;
    time = trandevice->time;
    double Iset;

    Iset = 0.95*trandevice->zeroCur1[0] * trandevice->ct1;

    Ia = trandevice->phasor[3];
    Ib = trandevice->phasor[4];
    Ic = trandevice->phasor[5];

    Ia = phasorNumMulti(trandevice->ct1, Ia);
    Ib = phasorNumMulti(trandevice->ct1, Ib);
    Ic = phasorNumMulti(trandevice->ct1, Ic);

    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);



    if((phasorAbs(I0) > Iset) && (time > 0.1)){
        trandevice->zeroCurStartFlagh = 1;
        trandevice->backTime[0] = trandevice->time;
        tranWriteLog(trandevice, "高压侧零序保护启动");

    }

}

void zeroCurStartv(tranDevice* trandevice){
    Phasor Ia, Ib, Ic;
    Phasor I0;
    double time;
    time = trandevice->time;
    double Iset;

    Iset = 0.95*trandevice->zeroCur2[0] * trandevice->ct2;

    Ia = trandevice->phasor[9];
    Ib = trandevice->phasor[10];
    Ic = trandevice->phasor[11];

    Ia = phasorNumMulti(trandevice->ct2, Ia);
    Ib = phasorNumMulti(trandevice->ct2, Ib);
    Ic = phasorNumMulti(trandevice->ct2, Ic);

    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);

    if((phasorAbs(I0) > Iset) && (time > 0.1)){
        trandevice->zeroCurStartFlagv = 1;
        trandevice->backTime[1] = trandevice->time;
        tranWriteLog(trandevice, "中压侧零序保护启动");

    }


}

void zeroCurStartt(tranDevice* trandevice){
    Phasor Ia, Ib, Ic;
    Phasor I0;
    double time;
    time = trandevice->time;
    double Iset;

    Iset = 0.95*trandevice->zeroCur2[3] * trandevice->ct5;

    Ia = trandevice->phasor[21];
    Ib = trandevice->phasor[22];
    Ic = trandevice->phasor[23];

    Ia = phasorNumMulti(trandevice->ct5, Ia);
    Ib = phasorNumMulti(trandevice->ct5, Ib);
    Ic = phasorNumMulti(trandevice->ct5, Ic);

    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);

    if((phasorAbs(I0) > Iset) && (time > 0.1)){
        trandevice->zeroCurStartFlagv = 1;
        trandevice->backTime[2] = trandevice->time;
        tranWriteLog(trandevice, "公共绕组零序保护启动");

    }

}

void phaseCurStarth(tranDevice* trandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double time;
    time = trandevice->time;
    double Iset;

    Iset = 0.95*trandevice->compSet1[2] * trandevice->ct1;

    Ia = trandevice->phasor[3];
    Ib = trandevice->phasor[4];
    Ic = trandevice->phasor[5];

    Ia = phasorNumMulti(trandevice->ct1, Ia);
    Ib = phasorNumMulti(trandevice->ct1, Ib);
    Ic = phasorNumMulti(trandevice->ct1, Ic);

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > Iset) && (time > 0.1)){
        trandevice->phaCurStartFlagh = 1;
        trandevice->backTime[3] = trandevice->time;
        tranWriteLog(trandevice, "高压侧过流保护启动");

    }
}

void phaseCurStartv(tranDevice* trandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double time;
    time = trandevice->time;
    double Iset;

    Iset = 0.95*trandevice->compSet2[2] * trandevice->ct2;

    Ia = trandevice->phasor[9];
    Ib = trandevice->phasor[10];
    Ic = trandevice->phasor[11];

    Ia = phasorNumMulti(trandevice->ct2, Ia);
    Ib = phasorNumMulti(trandevice->ct2, Ib);
    Ic = phasorNumMulti(trandevice->ct2, Ic);

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > Iset) && (time > 0.1)){
        trandevice->phaCurStartFlagv = 1;
        trandevice->backTime[4] = trandevice->time;
        tranWriteLog(trandevice, "中压侧过流保护启动");

    }
}


void phaseCurStartl1(tranDevice* trandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double time;
    time = trandevice->time;
    double Iset;

    Iset = 0.95*trandevice->compSet3[2] * trandevice->ct3;

    Ia = trandevice->phasor[15];
    Ib = trandevice->phasor[16];
    Ic = trandevice->phasor[17];

    Ia = phasorNumMulti(trandevice->ct3, Ia);
    Ib = phasorNumMulti(trandevice->ct3, Ib);
    Ic = phasorNumMulti(trandevice->ct3, Ic);

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > Iset) && (time > 0.1)){
        trandevice->phaCurStartFlagl1 = 1;
        trandevice->backTime[5] = trandevice->time;
        tranWriteLog(trandevice, "低压侧过流保护启动");

    }
}

void phaseCurStartl2(tranDevice* trandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double time;
    time = trandevice->time;
    double Iset;

    Iset = 0.95*trandevice->compSet4[2] * trandevice->ct4;

    Ia = trandevice->phasor[18];
    Ib = trandevice->phasor[19];
    Ic = trandevice->phasor[20];

    Ia = phasorNumMulti(trandevice->ct4, Ia);
    Ib = phasorNumMulti(trandevice->ct4, Ib);
    Ic = phasorNumMulti(trandevice->ct4, Ic);

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > Iset) && (time > 0.1)){
        trandevice->phaCurStartFlagl2 = 1;
        trandevice->backTime[6] = trandevice->time;
        tranWriteLog(trandevice, "低绕组过流保护启动");

    }

}

void tranPTerror(tranDevice* trandevice){

    double time;
    time = trandevice->time;

    Phasor Ua1, Ub1, Uc1, Uh1, Uh2;
    Phasor Ia1, Ib1, Ic1;
    double Ihmax, Ihe, Uh3;
    Ihe = 0.04*(trandevice->ratedI1);
    Ua1 = trandevice->phasor[0];
    Ub1 = trandevice->phasor[1];
    Uc1 = trandevice->phasor[2];
    Ia1 = trandevice->phasor[3];
    Ib1 = trandevice->phasor[4];
    Ic1 = trandevice->phasor[5];
    Ihmax = phasorAbs(Ia1);
    if(Ihmax < phasorAbs(Ib1)){
        Ihmax = phasorAbs(Ib1);
    }
    if(Ihmax < phasorAbs(Ic1)){
        Ihmax = phasorAbs(Ic1);
    }

    Uh1 = phasorSeq(Ua1, Ub1, Uc1, 1);
    Uh2 = phasorSeq(Ua1, Ub1, Uc1, 2);
    Uh3 = phasorAbs(trandevice->thrPhasor[0]);
    if (Uh3 < phasorAbs(trandevice->thrPhasor[1])){
        Uh3 = phasorAbs(trandevice->thrPhasor[1]);
    }
    if (Uh3 < phasorAbs(trandevice->thrPhasor[2])){
        Uh3 = phasorAbs(trandevice->thrPhasor[2]);
    }

    if ((((phasorAbs(Uh1) < 30) && (Ihmax > Ihe)) || (phasorAbs(Uh2) > 8) || (Uh3 > 10)) && (time > 0.1)){
        trandevice->ptFlag == 1;
        tranWriteLog(trandevice, "PT异常");
    }

    Phasor Ua2, Ub2, Uc2, Um1, Um2;
    Phasor Ia2, Ib2, Ic2;
    double Immax, Ime, Um3;
    Ime = 0.04*(trandevice->ratedI2);
    Ua2 = trandevice->phasor[6];
    Ub2 = trandevice->phasor[7];
    Uc2 = trandevice->phasor[8];
    Ia2 = trandevice->phasor[9];
    Ib2 = trandevice->phasor[10];
    Ic2 = trandevice->phasor[11];
    Immax = phasorAbs(Ia2);
    if(Immax < phasorAbs(Ib2)){
        Immax = phasorAbs(Ib2);
    }
    if(Immax < phasorAbs(Ic2)){
        Immax = phasorAbs(Ic2);
    }
    Um1 = phasorSeq(Ua2, Ub2, Uc2, 1);
    Um2 = phasorSeq(Ua2, Ub2, Uc2, 2);
    Um3 = phasorAbs(trandevice->thrPhasor[6]);
    if (Um3 < phasorAbs(trandevice->thrPhasor[7])){
        Um3 = phasorAbs(trandevice->thrPhasor[7]);
    }
    if (Um3 < phasorAbs(trandevice->thrPhasor[8])){
        Um3 = phasorAbs(trandevice->thrPhasor[8]);
    }

    if ((((phasorAbs(Um1) < 30) && (Ihmax > Ime)) || (phasorAbs(Um2) >8) || (Um3 > 10)) && (time > 0.1)){
        trandevice->ptFlag == 1;
        tranWriteLog(trandevice, "PT异常");
    }

    Phasor Ua3, Ub3, Uc3, Ul1, Ul2;
    Phasor Ia3, Ib3, Ic3;
    double Ilmax, Ile, Ul3;
    Ile = 0.04*(trandevice->ratedI3);
    Ua3 = trandevice->phasor[12];
    Ub3 = trandevice->phasor[13];
    Uc3 = trandevice->phasor[14];
    Ia3 = trandevice->phasor[15];
    Ib3 = trandevice->phasor[16];
    Ic3 = trandevice->phasor[17];
    Ilmax = phasorAbs(Ia3);
    if(Ilmax < phasorAbs(Ib3)){
        Ilmax = phasorAbs(Ib3);
    }
    if(Ilmax < phasorAbs(Ic3)){
        Ilmax = phasorAbs(Ic3);
    }
    Ul1 = phasorSeq(Ua3, Ub3, Uc3, 1);
    Ul2 = phasorSeq(Ua3, Ub3, Uc3, 2);
    Ul3 = phasorAbs(trandevice->thrPhasor[12]);
    if (Ul3 < phasorAbs(trandevice->thrPhasor[13])){
        Ul3 = phasorAbs(trandevice->thrPhasor[13]);
    }
    if (Ul3 < phasorAbs(trandevice->thrPhasor[14])){
        Ul3 = phasorAbs(trandevice->thrPhasor[14]);
    }

    if ((((phasorAbs(Ul1) < 30) && (Ilmax > Ile)) || (phasorAbs(Ul2) >8) || (Ul3 > 10)) && (time > 0.1)){

        trandevice->ptFlag == 1;
        tranWriteLog(trandevice, "PT异常");
    }


}

void tranCTerror(tranDevice* trandevice){
    double time;
    time = trandevice->time;

    int stopFlag;
    int startFlag;
    startFlag = trandevice->lonDiffStartFlag + trandevice->deltaDiffStartFlag + trandevice->zeroDiffStartFlag + trandevice->splitDiffStartFlag;
    startFlag = startFlag + trandevice->lowDiffStartFlag + trandevice->phaseLonDiffStartFlag + trandevice->phaseDeltaStartFlag;

//    Phasor Ua1, Ub1, Uc1, Uh1, Uh2;
    Phasor Ia1, Ib1, Ic1;
    Phasor Ia1m, Ib1m, Ic1m;
    double Ihmax, Ihe;
    Ihe = trandevice->ratedI1;
    if(trandevice->Idt > 1.2*Ihe){
        stopFlag = 1;
    }

    Ia1 = trandevice->phasor[3];
    Ib1 = trandevice->phasor[4];
    Ic1 = trandevice->phasor[5];

    Ia1m = trandevice->memoryIa1[0];
    Ib1m = trandevice->memoryIb1[0];
    Ic1m = trandevice->memoryIc1[0];

    int curFlag;
    if(phasorAbs(Ia1) > 1.1*phasorAbs(Ia1m)){
        curFlag == 1;
    }
    if(phasorAbs(Ib1) > 1.1*phasorAbs(Ib1m)){
        curFlag == 1;
    }
    if(phasorAbs(Ic1) > 1.1*phasorAbs(Ic1m)){
        curFlag == 1;
    }


    Ihmax = phasorAbs(Ia1);
    if(Ihmax < phasorAbs(Ib1)){
        Ihmax = phasorAbs(Ib1);
    }
    if(Ihmax < phasorAbs(Ic1)){
        Ihmax = phasorAbs(Ic1);
    }
    if(Ihmax > 1.1 * Ihe){
        curFlag ==1;
    }


    Phasor Ia2, Ib2, Ic2;
    Phasor Ia2m, Ib2m, Ic2m;
    double Immax, Ime;
    Ime = trandevice->ratedI2;

    Ia2 = trandevice->phasor[9];
    Ib2 = trandevice->phasor[10];
    Ic2 = trandevice->phasor[11];

    Ia2m = trandevice->memoryIa2[0];
    Ib2m = trandevice->memoryIb2[0];
    Ic2m = trandevice->memoryIc2[0];

    if(phasorAbs(Ia2) > 1.1*phasorAbs(Ia2m)){
        curFlag == 1;
    }
    if(phasorAbs(Ib2) > 1.1*phasorAbs(Ib2m)){
        curFlag == 1;
    }
    if(phasorAbs(Ic2) > 1.1*phasorAbs(Ic2m)){
        curFlag == 1;
    }


    Immax = phasorAbs(Ia2);
    if(Immax < phasorAbs(Ib2)){
        Immax = phasorAbs(Ib2);
    }
    if(Immax < phasorAbs(Ic2)){
        Immax = phasorAbs(Ic2);
    }
    if(Immax > 1.1 * Ime){
        curFlag ==1;
    }

    Phasor Ia3, Ib3, Ic3;
    Phasor Ia3m, Ib3m, Ic3m;
    double Ilmax, Ile;
    Ile = trandevice->ratedI3;

    Ia3 = trandevice->phasor[15];
    Ib3 = trandevice->phasor[16];
    Ic3 = trandevice->phasor[17];

    Ia3m = trandevice->memoryIa3[0];
    Ib3m = trandevice->memoryIb3[0];
    Ic3m = trandevice->memoryIc3[0];

    if(phasorAbs(Ia3) > 1.1*phasorAbs(Ia3m)){
        curFlag == 1;
    }
    if(phasorAbs(Ib3) > 1.1*phasorAbs(Ib3m)){
        curFlag == 1;
    }
    if(phasorAbs(Ic3) > 1.1*phasorAbs(Ic3m)){
        curFlag == 1;
    }


    Ilmax = phasorAbs(Ia3);
    if(Ilmax < phasorAbs(Ib3)){
        Ilmax = phasorAbs(Ib2);
    }
    if(Ilmax < phasorAbs(Ic3)){
        Ilmax = phasorAbs(Ic3);
    }
    if(Ilmax > 1.1 * Ile){
        curFlag ==1;
    }

    if((curFlag < 1) && (startFlag < 1) && (stopFlag > 0) && (time > 0.1)){

        trandevice->ctFlag == 1;
        tranWriteLog(trandevice, "CT断线");
    }


}
